//
//  ViewController.swift
//  OneSecond
//
//  Created by Qingke on 16/3/31.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var disPlayLabel: UILabel!
    @IBOutlet weak var disPlayBackgroundLabel: UIImageView!
    @IBOutlet weak var TryCountLable: UILabel!
    @IBOutlet weak var dashenTryCountLabel: UILabel!
    @IBOutlet weak var fanrenTryCountLabel: UILabel!
    @IBOutlet weak var touchOutletButton: UIButton!
    let touchBackColor1 = UIImage(named: "指纹Y")
    let touchBackColor2 = UIImage(named: "指纹R")
    let displayBackColor = UIImage(named: "cool_bg")
    var timer:  NSTimer!
    
    var dashencount = 0
    var fanrenCount = 0
    var trytotalCount = 0
    var totalTime = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        touchOutletButton.addTarget(self, action: "touchBegin", forControlEvents: .TouchDown)
        touchOutletButton.addTarget(self, action: "touchEnd", forControlEvents: .TouchUpOutside)
        touchOutletButton.addTarget(self, action: "touchEnd", forControlEvents: .TouchUpInside)
    
    }
    func timerFire(){
        totalTime += 0.01
        
    }
    func touchBegin(){
        timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector:  Selector("timerFire"), userInfo: nil, repeats: true)
        
    }
    func touchEnd(){
        timer.invalidate()
        if totalTime > 1 {
            disPlayBackgroundLabel.image = UIImage(named: "cool_bg")
            fanrenCount++
        }else if totalTime >= 0.9{
            disPlayBackgroundLabel.image = UIImage(named: "active_bg")

            dashencount++
        }else {
            fanrenCount++
            disPlayBackgroundLabel.image = UIImage(named: "active_bg")

        }
        trytotalCount++
        display()
        totalTime = 0
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func touchButton(sender: UIButton) {
        
        sender.setBackgroundImage(touchBackColor1, forState: .Normal)
        sender.setBackgroundImage(touchBackColor2, forState: .Highlighted)
        
        
        
        trytotalCount++
        
    }
    func display(){
        dashenTryCountLabel.text = "大神： \(dashencount)次"
        fanrenTryCountLabel.text = "凡人： \(fanrenCount)次"
        TryCountLable.text = "你已经尝试了： \(trytotalCount)次"
        disPlayLabel.text =  String(format: "%.2f", totalTime) + "秒"
    }
}

